<?php
/**
 * @title 78. 子集 - 二进制
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     */
    function subsets($nums) {
        /**
         * @since 2020-09-21 结果集
         */
        $result = [];

        /**
         * @since 2020-09-21 数组长度
         */
        $len = count($nums);

        /**
         * @since 2020-09-21 二进制
         */
        for ($i=0, $iMax = (1<<$len)-1; $i<=$iMax; $i++){
            $bin = strval(decbin($i));

            /**
             * @since 2020-09-21 逐位取值
             */
            $data = [];
            for($j=strlen($bin)-1, $k=$len-1; $j>=0; $j--, $k--){
                if($bin[$j] == 1){
                    $data[] = $nums[$k];
                } else {}
            }

            /**
             * @since 2020-09-21 赋值
             */
            $result[] = $data;
        }


        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.07_7.php
 */
$datas = [
    [1,2,3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->subsets($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
